#include "head.h"


void *thread_main(void *p){
    thread_pool_t *tp = (thread_pool_t*)p;

    //创建数据库连接
    MYSQL *mysql_con;
    int num = server_mysql_init(&mysql_con);


    char *buf = "select * from user_info where id = 1";
    int ret = mysql_select_file_is_exist(mysql_con, buf);

    char str[20] = { 0 };
    mysql_select_get_one_str(mysql_con, "select user_name from user_info where id = 1", str);

    char str1[2][1024] = { 0 };
    mysql_select_get_one_row(mysql_con, "select id, user_name from user_info where id = 1", str1);
    for(int i = 0; i < 2; i++){
        printf("row[%d]:%s\n", i, str1[i]);
    }


    while(1){
        //线程获得锁准备工作
        int ret = pthread_mutex_lock(&(tp->lock));
        ERROR_CHECK(ret, -1, "pthread_mutex_lock_threadMain.c");

        //当任务队列中没有任务时，线程挂起等待
        while(tp->queue.queue_size <= 0){
            ret = pthread_cond_wait(&(tp->cond), &(tp->lock));
            ERROR_CHECK(ret, -1, "pthread_cond_wait_threadMain.c");
        }

        //任务队列中有任务了
        int netFD = tp->queue.head->netFD;
        //出队列
        dequeue(&(tp->queue));

        //释放锁，让给其他等待工作的线程
        ret = pthread_mutex_unlock(&(tp->lock));

        printf("线程%lu开始工作\n", pthread_self());
        //线程获得任务开始工作
        start_work(netFD, mysql_con);
        printf("线程%lu结束工作\n", pthread_self());

    }
}



